#include "process.h"

const char *str = "| / - \\";

void process_v1()
{
    // version 1
    int rate = 0; // 比率

    char bar[SIZE];
    memset(bar, '\0', sizeof(bar));
    int num = strlen(str);
    while (rate <= MAX_RATE)
    {
        printf("[%-100s][%d%%][%c]\r", bar, rate, str[rate % num]);
        usleep(STIME);
        fflush(stdout);
        bar[rate++] = STYLE;
    }
    printf("\n");
}

// 不能一次将进度条打印完毕，否则无法平滑的和场景结合
// 该函数，应该根据rate，自动的打一次
void process_v2(int rate)
{
    // version 2
    static char bar[SIZE] = {0};
    int num = strlen(str);
    if (rate <= MAX_RATE && rate >= 0)
    {
        printf("[%-100s][%d%%][%c]\r", bar, rate, str[rate % num]);
        fflush(stdout);
        bar[rate] = STYLE;
    }
    if (rate == MAX_RATE)
        memset(bar, '\0', sizeof(bar));
}

void process_v3(double rate)
{
    // version 3
    static char bar[SIZE] = {0};
    static int cnt = 0;
    int num = strlen(str);
    if (rate <= MAX_RATE && rate >= 0)
    {
        cnt++;
        cnt = (cnt >= num ? 0 : cnt);
        printf("[%-100s][%.1f%%][%c]\r", bar, rate, str[cnt]);
        fflush(stdout);
        if (rate < MAX_RATE)
        {
            bar[(int)rate] = STYLE_BODY;          //=
            bar[(int)rate + 1] = STYLE_HEADER;    //>
        }
        else 
        {
            bar[(int)rate] = STYLE_BODY;
        }
    }
}